package com.cv.infra.api.file;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.cv.infra.dal.dataobject.file.InfraFileDO;
import com.cv.infra.dal.mapper.InfraFileMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 文件 API 接口实现类
 *
 * @author Charles_XDXD
 */
@Service
@DubboService
public class FileApiServiceImpl implements FileApi {

    @Resource
    private InfraFileMapper infraFileMapper;

    @Override
    public String getUrl(Long id) {
        if (ObjUtil.isEmpty(id)) {
            return StrUtil.EMPTY;
        }

        return Optional.ofNullable(infraFileMapper.selectById(id))
                .orElse(new InfraFileDO().setPath(StrUtil.EMPTY))
                .getPath();
    }

    @Override
    public boolean isTranscoded(Long id) {
        if (ObjUtil.isEmpty(id)) {
            return false;
        }

        return ObjUtil.isNotEmpty(getUrl(id));
    }

}
